/*
 *  Copyright 2006 www.webtair.com
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.webtair.sessionman.example;

import com.webtair.session.*;
import com.webtair.session.artifact.*;

import com.webtair.sessionman.ws.types.IdSessionDocument;
import com.webtair.sessionman.ws.types.IsOkElementDocument;
import com.webtair.sessionman.ws.types.MessageDocument;
import com.webtair.sessionman.ws.types.UserAuthElementDocument;
import com.webtair.sessionman.ws.types.UserAuthElementDocument.UserAuthElement;

/**
 * Service class of SessionManExample webservice
 * it can work under Axis2-1.* form configuration information
 * please see the /config/services.xml
 * 
 * @author Vaclav (Vyacheslav Yakovenko)
 */
public class SessionManExampleService extends SessionManExampleServiceSkeleton {
	
	/** Message indicating that session is alive */
	private static final String SESSION_IS_ALIVE = "Session is alive!";
	
	/** Message indicating that session is expired alredy */
	private static final String SESSION_IS_EXPIRED = "Session is expired!";
	
	/** SessionMan instance - main goal of this example */
	private static SessionMan<SimpleSessionInfo> sessions =
		new SessionMan<SimpleSessionInfo>();

	/**
	 * Method that allow for client to login under this ws
	 * @return IdSessionDocument - soap envelope with session id;
	 */
	@Override
	public IdSessionDocument login(UserAuthElementDocument userAuthElementDocument) {

		/* default login and pass for examples only at this place you 
		 * mast recieve real pass from the real storage like RDBMS or 
		 * config-file or somthing else.
		 */
		String defaultLogin = "sessionman";
		String defaultpass = "example";
		
		UserAuthElement userAuthElement = 
			userAuthElementDocument.getUserAuthElement();
		
		String idSession = null;
		if (userAuthElement.getLogin().equals(defaultLogin) &&
				userAuthElement.getPassword().equals(defaultpass)){
			// request for id session from SessionMan lib 
			idSession = sessions.putSessionInfo(userAuthElement.getLogin(),
					new SimpleSessionInfo());
		}
		
		// preparing soap env for client
		IdSessionDocument idSessionDocument = IdSessionDocument.Factory.newInstance();
		idSessionDocument.setIdSession(idSession);
		return idSessionDocument;
	}


	/**
	 * Method remove sessions from SesionMan instance
	 * @param IdSessionDocument - session id that must be removed;
	 * @return IsOkElementDocument - true if session was seccessfuly 
	 * removed from SessionMan instance;
	 */
	@Override
	public IsOkElementDocument disconnect(IdSessionDocument idSessionDocument) {
		String idSession = idSessionDocument.getIdSession();
		sessions.removeSession(idSession);
		
		IsOkElementDocument isOkElementDocument = 
			IsOkElementDocument.Factory.newInstance();
		isOkElementDocument.setIsOkElement(true);
		
		return isOkElementDocument;
	}

	/**
	 * Ping method - example of some function that can be implemented
	 * by you in real ws.
	 * @param IdSessionDocument - session id that expected to be alive;
	 * @return MessageDocument - some ping message;
	 */
	@Override
	public MessageDocument ping(IdSessionDocument idSessionDocument) {
		
		MessageDocument messageDocument = 
			MessageDocument.Factory.newInstance();

		String idSession = idSessionDocument.getIdSession();
		
		if (sessions.isSessionExpired(idSession)){
			messageDocument.setMessage(SESSION_IS_EXPIRED);
		}else{
			messageDocument.setMessage(SESSION_IS_ALIVE);
		}
		
		return messageDocument;
	}
}